package com.zhiyou100.loans.dao.impl;

import java.sql.ResultSet;

import com.zhiyou100.loans.dao.LoanbillDao;
import com.zhiyou100.loans.model.Loanbill;
import com.zhiyou100.loans.util.JDBCUtil;

public class LoanbillDaoImpl implements LoanbillDao {
	JDBCUtil util = new JDBCUtil();
	@Override
	public int add(Loanbill bill) {
		String sql="insert into loanbill(typesid,money,rateid,starttime,endtime,messagestate,userid,successstate,creditid) values(?,?,?,?,?,?,?,?,?)";
		Object[] objs={bill.getTypesid(),bill.getMoney(),bill.getRateid(),bill.getStarttime(),bill.getEndtime(),bill.getMessagestate(),bill.getUserid(),bill.getSuccessstate(),bill.getCreditid()};
		return util.update(sql, objs);
	}
	@Override
	public ResultSet selectLoanbillInfo() {
		//消费类型、借款金额、时长、起始时间、截止时间，月利息、信誉等级、输入你的借款金额、操作.隐藏借款人信息
		String sql="select a.*,sum(lendbill.money) lendmoney from (select loanbill.id id,money,starttime,endtime,userid,duration,monthrate,name,grade,messagestate from loanbill,credit,rate,types where  loanbill.rateid=rate.id and loanbill.typesid=types.id and loanbill.creditid=credit.id)a left join lendbill on a.id = lendbill.loanbillid where a.messagestate=1 group by(a.id)";
		Object[] objs={};
		return util.query(sql, objs);
	}
	@Override
	public ResultSet selectByUserid(int parseInt) {
		//借款信息已失效或者借款已还清，可以再次借款(借款信息生效)
		String sql="select id from loanbill where (userid=? and messagestate=1) or (userid=? and successstate=1)";
		Object[] objs={parseInt,parseInt};
		return util.query(sql, objs);
	}
	@Override
	public ResultSet selectlbip(int parseInt) {
		//消费类型、借款金额、时长、起始时间、截止时间，月利息、信誉等级、输入你的借款金额、操作.隐藏借款人信息
		String sql="select loanbill.id id,money,starttime,endtime,userid,duration,monthrate,name,grade,succstarttime,succendtime,successstate from loanbill,credit,rate,types where  loanbill.rateid=rate.id and loanbill.typesid=types.id and loanbill.creditid=credit.id and loanbill.userid=? and loanbill.successstate<>2";
		Object[] objs={parseInt};
		return util.query(sql, objs);
	}
	@Override
	public ResultSet selectMonthByLbid(int parseInt) {
		String sql = "select duration,starttime,endtime,loanbill.id,typesid,messagestate,money,userid,succstarttime,successstate,succendtime,rateid,creditid,monthrate from loanbill left join rate on loanbill.rateid=rate.id where loanbill.id=?";
		Object[] objs={parseInt};
		return util.query(sql, objs);
	}
	@Override
	public int update(Loanbill lb) {
		String sql="update loanbill set messagestate=?,successstate=?,succstarttime=?,succendtime=? where id=?";
		Object[] objs={lb.getMessagestate(),lb.getSuccessstate(),lb.getSuccstarttime(),lb.getSuccendtime(),lb.getId()};
		return util.update(sql, objs);
	}

}
